package com.gateway.jug.gps;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.gateway.jug.R;
import com.gateway.jug.interfaces.FuzzInfo;
import com.gateway.jug.interfaces.IFuzzInfoCallback;
import com.gateway.jug.interfaces.IRemoteRegistration;

public class FuzzStats extends Activity {

	public static final String TAG = "FuzzStats";
	
	private EditText hSpeedText;
	private EditText pSpeedText;
	private EditText latitudeText;
	private EditText longitudeText;
	private EditText hDistText;
	private EditText pDistText;

	IRemoteRegistration mService;
    
    ConnectionManager mRemoteConnection;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(com.gateway.jug.R.layout.statspanel);

		hSpeedText = (EditText) findViewById(R.id.hspeed);		
		pSpeedText = (EditText) findViewById(R.id.pspeed);
		latitudeText = (EditText) findViewById(R.id.latitude);
		longitudeText = (EditText) findViewById(R.id.longitude);
		hDistText = (EditText) findViewById(R.id.hdist);
		pDistText = (EditText) findViewById(R.id.pdist);

		bindService(new Intent("com.gateway.jug.interfaces.IRemoteRegistration"), getConnectionManager(), Context.BIND_AUTO_CREATE);
        Log.d(TAG, "Bound to remote service.");

	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
    	unbindService(getConnectionManager());        	
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
        bindService(new Intent("com.gateway.jug.interfaces.IRemoteRegistration"), getConnectionManager(), Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}	

	private ConnectionManager getConnectionManager() {
		if (mRemoteConnection == null) {
			mRemoteConnection = new ConnectionManager();			
		}
		return mRemoteConnection;
	}
	
    private class ConnectionManager implements ServiceConnection {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mService = IRemoteRegistration.Stub.asInterface(service);

            try {
                mService.registerCallback(mCallback);
            } catch (RemoteException e) { // no need to perform any handling
            	Log.e(TAG, "Error occured, should be benign", e);
            }

            // As part of the sample, tell the user what happened.
            Toast.makeText(FuzzStats.this, R.string.fuzz_service_client_connected,
                    Toast.LENGTH_SHORT).show();
        }

        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been
            // unexpectedly disconnected -- that is, its process crashed.
            mService = null;

            // As part of the sample, tell the user what happened.
            Toast.makeText(FuzzStats.this, R.string.fuzz_service_client_disconnected,
                    Toast.LENGTH_SHORT).show();
        }
    };    

    private IFuzzInfoCallback mCallback = new IFuzzInfoCallback.Stub() {

		@Override
		public void updateInfo(FuzzInfo info) throws RemoteException {
            mHandler.sendMessage(mHandler.obtainMessage(STATS_MSG, info));			
		}
    	
    };

    private void clearFields() {
    	hDistText.setText("");
    	hSpeedText.setText("");
    	latitudeText.setText("");
    	longitudeText.setText("");
    	pDistText.setText("");
    	pSpeedText.setText("");
    }
    
    private void setData(FuzzInfo fi) {
    	hDistText.setText(fi.totalHDistance + "");
    	hSpeedText.setText(fi.hspeed + "");
    	latitudeText.setText(fi.currentlat + "");
    	longitudeText.setText(fi.currentlong + "");
    	pDistText.setText(fi.totalPDistance + "");
    	pSpeedText.setText(fi.pspeed + "");
    }
    
    private static final int STATS_MSG = 1;

    private Handler mHandler = new Handler() {
        @Override public void handleMessage(Message msg) {
            switch (msg.what) {
                case STATS_MSG:
                	if (msg.obj != null) {
                		FuzzInfo fi = (FuzzInfo) msg.obj;
                		setData(fi);
                	}
                    break;
                default:
                    super.handleMessage(msg);
            }
        }

    };
	    
}
